import {Inject, Provide} from '@midwayjs/decorator';
import {InjectEntityModel} from "@midwayjs/orm";
import {User} from "../entity/user";
import {Repository} from "typeorm";
import CommonService from "../../common/service/commonService";

@Provide()
export default class UserService {
  @InjectEntityModel(User)
  userModel: Repository<User>;
  @Inject()
  commonService: CommonService;
  async getList() {
    let userList = await this.userModel.find()
    return userList;
  }

  async save(data) {
    let rs:any='';
    // if(!data.id || data.id.length==0){
    //   data.id = this.commonService.getUUID();
    // }
    rs=await this.userModel.save(data);

    if(rs && Object.keys(rs).length>0){
      return {
        success: true,
        msg:'保存成功'
      }
    }else{
      return {
        success: false,
        msg:'保存失败'
      }
    }
  }

  public async delete(id:string) {
    // @ts-ignore
    let removeObj = await this.userModel.findOne({id:id});
    let rs=await this.userModel.remove(removeObj).then(res=>{
      return {
        success: true,
        msg:'删除成功！'
      }
    }).catch(err=>{
      return {
        success: false,
        msg:'删除失败！'
      }
    });
    return rs
  }
}
